import QtQuick
import QtQuick.Controls.Material
import Qt5Compat.GraphicalEffects

Item {
    width: 90
    height: 90

    property bool isSelected: false
    property bool isMousePressed: false
    property bool buttonEnable: true

    // 简化颜色属性 - 只保留正常和按下状态
    property string normalColor: "transparent"  // 透明背景
    property string pressedColor: "#778899"    // 按下时的颜色

    property string selectedColor: "#778899"
    property string selectedBorderColor: "#3498db"
    property string disabledColor: "#f0f0f0"
    property string disabledBorderColor: "#cccccc"
    property string disabledTextColor: "#999999"

    property string normalBorderColor: "transparent"  // 透明边框
    property string pressedBorderColor: "#3498db"    // 按下时的边框颜色

    // 文字属性
    property real contentText_Fontsize: 15
    property string contentText_Data: "按钮"
    property string contentText_Color: "white"
    property bool contentText_Bold: false

    // 图片属性
    property string imageSource: ""
    property real imageWidth: 60
    property real imageHeight: 60
    property string imageFillMode: "PreserveAspectFit"

    // 简化ColorOverlay颜色属性 - 只保留正常和按下状态
    property color normalImageColor: "#3CB371"
    property color pressedImageColor: "#DAA520"
    property color selectImageColor: "#DAA520"
    property color disabledImageColor: "#B3AAAAAA"

    // 布局属性
    property real imageTopMargin: 12
    property real textBottomMargin: 8
    property real contentSpacing: 4

    property real contentRectangle_BorderWidth: 2
    property real borderRadius: 8

    signal buttonClicked()
    signal buttonPressed()
    signal buttonReleased()
    signal buttonPressedAndHold()

    // 鼠标事件处理
    MouseArea {
        id: mainMouseArea
        anchors.fill: parent
        hoverEnabled: false  // 禁用悬停检测，因为不再需要悬停状态
        enabled: buttonEnable

        onPressed: {
            isMousePressed = true
            buttonPressed()
        }

        onReleased: {
            if (isMousePressed) {
                isMousePressed = false
                buttonReleased()
            }
        }

        onClicked: {
            forceActiveFocus()
            buttonClicked()
        }
        onPressAndHold: buttonPressedAndHold()
    }

    // 视觉组件
    Rectangle {
        id: visualButton
        anchors.fill: parent
        radius: borderRadius
        border.width: contentRectangle_BorderWidth
        // 简化边框颜色逻辑 - 只有正常和按下状态
        border.color: !buttonEnable ? disabledBorderColor
                    : isSelected ? selectedBorderColor
                    : isMousePressed ? pressedBorderColor : normalBorderColor
        // 简化背景颜色逻辑 - 只有正常和按下状态
        color: !buttonEnable ? disabledColor
                : isSelected ? selectedColor
                : isMousePressed ? pressedColor : normalColor


        // 图片区域 - 顶部居中
        Item {
            id: imageContainer
            width: imageWidth
            height: imageHeight + imageTopMargin
            anchors.horizontalCenter: parent.horizontalCenter

            // 原始图片（作为ColorOverlay的源）
            Image {
                id: originalImage
                anchors.top: parent.top
                // anchors.topMargin: 5
                anchors.horizontalCenter: parent.horizontalCenter
                width: imageWidth
                height: imageHeight
                source: imageSource
                fillMode: {
                    switch(imageFillMode) {
                    case "Stretch": return Image.Stretch;
                    case "PreserveAspectFit": return Image.PreserveAspectFit;
                    case "PreserveAspectCrop": return Image.PreserveAspectCrop;
                    default: return Image.PreserveAspectFit;
                    }
                }
                visible: false
                asynchronous: true
            }

            // ColorOverlay效果 - 简化颜色状态逻辑
            ColorOverlay {
                id: imageOverlay
                anchors.fill: originalImage
                source: originalImage
                color: !buttonEnable ? disabledImageColor
                       : isSelected ? selectImageColor
                       : isMousePressed ? pressedImageColor : normalImageColor
                cached: true
            }
        }

        Rectangle{
            width: 80
            height: 20
            anchors.bottom: visualButton.bottom
            anchors.bottomMargin: 5
            anchors.horizontalCenter: visualButton.horizontalCenter
            color: "transparent"

            Text {
                id: buttonText
                text: qsTr(contentText_Data)
                font.pixelSize: contentText_Fontsize
                font.bold: contentText_Bold
                color: !buttonEnable ? disabledTextColor : contentText_Color
                // horizontalAlignment: Text.AlignHCenter
                // verticalAlignment: Text.AlignVCenter
                anchors.centerIn: parent
                // width: Math.min(implicitWidth, parent.width - 10)
                elide: Text.ElideRight
                maximumLineCount: 1
            }

        }

    }
}

